package com.bk.bookviews.controller;

import com.bk.bookviews.domain.ResultMsg;
import com.bk.bookviews.domain.Title;
import com.bk.bookviews.exception.BusinessException;
import com.bk.bookviews.exception.BusinessExceptionEnum;
import com.bk.bookviews.service.TitleService;
import com.bk.bookviews.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/bk/title")
public class TitleController {
    @Autowired
    TitleService titleService;

    @GetMapping()
    public ResultMsg selectTitleAll() {
        return ResultMsg.successResult(titleService.selectTitleList());
    }

    @PostMapping("/save")
    public ResultMsg save(@RequestBody Title title) throws BusinessException {
        if (StringUtil.isBlank(title.getTitleName())){
            throw new BusinessException(BusinessExceptionEnum.TITLE_NAME_IS_NULL);
        }
        titleService.saveTitle(title);
        return ResultMsg.successResult();
    }

    @DeleteMapping()
    public ResultMsg deleteTitle(String titleId) {
        titleService.delete(titleId);
        return ResultMsg.successResult();
    }
}
